var UploadFile = require ("../../Controls/UploadFile")
var util = require ("../../utils/util")
var DataExchange = require ("../../Controls/DataExchange")
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
var app = getApp()

function getPublish_txt()
{
  var ret = "";
  if (app.globalData.credit.publishTimes == 0)
  {
    ret = "支付并发布秘袋";
  }
  else
  {
    ret = `发布秘袋（${app.globalData.credit.publishTimes}）`;
  }


  return ret;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
      images: [],
      publish_txt:"发布秘袋",
      indicatorDots:true,
      autoPlay:false,
      interval:1000,
      duration:300,
      times:[
        { key: 1, value: 4, checked: false },
        { key: 2, value: 8, checked: false },
        { key: 3, value: 12, checked: false },
        { key: 4, value: 24, checked: true },
      ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.ID);
    console.log(options.index);
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ publish_txt: getPublish_txt() });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  radioChange:function(event)
  {
    var tiems = this.data.times;
    var selectConfig = event.currentTarget.dataset.config;
    for (var i = 0; i < tiems.length; i++)
    {
      tiems[i].checked = false;
      if (tiems[i].key == selectConfig.key)
        {
          selectConfig.checked = true;
          tiems[i] = selectConfig;
        }
    }

    this.setData({ times: tiems});
  },
  btnAddimgIvk:function()
  {

    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original"],
      sourceType: ["album", "camera"],
      success: function (res) {
        //that.setData({images:res.tempFilePaths});
        wx.navigateTo({
          url: `../../vendor/we-cropper/cutInside/cutInside?src=${res.tempFilePaths[0]}`,
        })
      }
    })
  }, 
  formSubmit:function(e)
  {
    var checkInput = function(kv)
    {
      if(kv.input_name.length == 0)
      {
        Toast('请输入秘袋名称');
        return false;
      }
      if (kv.input_price.length == 0) 
      {
        Toast('请输入秘袋价格');
        return false;
      }
      if (kv.input_count.length == 0) {
        Toast('请输入秘袋数量');
        return false;
      }
      if(kv.input_desc.length == 0)
      {
        Toast('请输入秘袋描述信息');
        return false;
      }

      return true;
    }

    if(!checkInput(e.detail.value))
    {
      return;
    }

    if(this.data.images.length == 0 )
    {
      Toast('请上传秘袋图片');
      return;
    }

    var images = this.data.images;
    var name = e.detail.value.input_name;
    var maxPrice = parseInt(e.detail.value.input_price);
    var minPrice = parseInt(0);
    var count = parseInt(e.detail.value.input_count);
    var desc = e.detail.value.input_desc;
    var duration = 0;
    for(var i = 0; this.data.times.length; i++)
    {
      if(this.data.times[i].checked)
      {
        duration = this.data.times[i].value * 3600;
        break;
      }
    }
    DataExchange.set_publishData(this.data.images, name, maxPrice, minPrice, count, desc, duration);

    if (app.globalData.credit.publishTimes == 0) {
      wx.navigateTo({
        url: "../buy/buy",
      })
    }
    else {
      wx.navigateTo({
        url: '../../pages/product/product?preview=true',
      })
    }
  }
})